home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_10 / phillip2 / objwarp.c < prev    next >
Text File  |  1994-03-02  |  6KB  |  231 lines

  1.  
  2.  
  3.      /*******************************************
  4.      *
  5.      *   object_warp(..
  6.      *
  7.      *   This routine warps a ROWSxCOLS section
  8.      *   of an image.  The il, ie parameters
  9.      *   specify which ROWSxCOLS section of
  10.      *   the image to warp.  The x_control and
  11.      *   y_control parameters are the control
  12.      *   points inside that section.  Therefore,
  13.      *   x_control and y_control will always be
  14.      *   less the COLS and ROWS.
  15.      *
  16.      *   The point coordinates are for the four
  17.      *   corners of a four side figure.
  18.      *      x1,y1     x2,y2
  19.      *
  20.      *      x4,y4     x3,y3
  21.      *
  22.      *******************************************/
  23.  
  24. object_warp(in_name, out_name, 
  25.             the_image, out_image,
  26.             il, ie, ll, le, 
  27.             x1, y1, x2, y2, x3, y3, x4, y4, 
  28.             bilinear)
  29.    char   in_name[], out_name[];
  30.    int    bilinear, il, ie, ll, le, 
  31.           x1, y1, x2, y2, x3, y3, x4, y4;
  32.    short  the_image[ROWS][COLS],
  33.           out_image[ROWS][COLS];
  34. {
  35.    int    extra_x = 0, 
  36.           extra_y = 0;
  37.  
  38.    struct tiff_header_struct image_header;
  39.  
  40.    create_file_if_needed(in_name, out_name, out_image);
  41.  
  42.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  43.  
  44.       /***********************************
  45.       *
  46.       *   Call the warp loop function you
  47.       *   need (with or without bilinear
  48.       *   interpolation).
  49.       *
  50.       ***********************************/
  51.  
  52.    if(bilinear)
  53.       bi_full_warp_loop(the_image, out_image,
  54.                         x1, x2, x3, x4,
  55.                         y1, y2, y3, y4,
  56.                         extra_x, extra_y);
  57.    else
  58.       full_warp_loop(the_image, out_image,
  59.                      x1, x2, x3, x4,
  60.                      y1, y2, y3, y4,
  61.                      extra_x, extra_y);
  62.  
  63.    write_array_into_tiff_image(out_name, out_image,
  64.                                il, ie, ll, le);
  65. }  /* ends object_warp */
  66.  
  67.  
  68.  
  69.  
  70.      /*******************************************
  71.      *
  72.      *   full_warp_loop(..
  73.      *
  74.      *   This routine sets up the coefficients
  75.      *   and loops through an entire
  76.      *   ROWSxCOLS image that is being warped.
  77.      *
  78.      *******************************************/
  79.  
  80. full_warp_loop(the_image, out_image,
  81.                x1, x2, x3, x4,
  82.                y1, y2, y3, y4,
  83.                extra_x, extra_y)
  84.    int    extra_x, extra_y,
  85.           x1, x2, x3, x4,
  86.           y1, y2, y3, y4;
  87.    short  the_image[ROWS][COLS],
  88.           out_image[ROWS][COLS];
  89. {
  90.    int    cols_div_2, denom, i, j, rows_div_2,
  91.           xa, xb, xab, x_out, ya, yb, yab, y_out;
  92.  
  93.    cols_div_2 = COLS/2;
  94.    rows_div_2 = ROWS/2;
  95.    denom      = cols_div_2 * rows_div_2;
  96.  
  97.       /***********************************
  98.      *
  99.      *   Set up the terms for the
  100.      *   spatial transformation.
  101.      *
  102.      ***********************************/
  103.  
  104.    xa  = x2 - x1;
  105.    xb  = x4 - x1;
  106.    xab = x1 - x2 + x3 - x4;
  107.  
  108.    ya  = y2 - y1;
  109.    yb  = y4 - y1;
  110.    yab = y1 - y2 + y3 - y4;
  111.  
  112.       /***********************************
  113.      *
  114.      *   Loop through the image and
  115.      *   perform the spatial
  116.      *   transformation.
  117.      *
  118.      ***********************************/
  119.  
  120.       /* NOTE a=j b=i */
  121.  
  122.    printf("\n");
  123.    for(i=0; i<ROWS; i++){
  124.       if( (i%10) == 0) printf("%d ", i);
  125.       for(j=0; j<COLS; j++){
  126.  
  127.          x_out = x1 + (xa*j)/COLS +
  128.                (xb*i)/ROWS + (xab*i*j)/(denom);
  129.          y_out = y1 + (ya*j)/COLS +
  130.                (yb*i)/ROWS + (yab*i*j)/(denom);
  131.  
  132.          if(x_out < 0       ||
  133.             x_out >= COLS   ||
  134.             y_out < 0       ||
  135.             y_out >= ROWS)
  136.             out_image[i+extra_y][j+extra_x] = FILL;
  137.          else
  138.             out_image[i+extra_y][j+extra_x] =
  139.              the_image[y_out][x_out];
  140.  
  141.       }  /* ends loop over j */
  142.    }  /* ends loop over i */
  143.  
  144. }   /* ends full_warp_loop */
  145.  
  146.  
  147.  
  148.  
  149.  
  150.      /*******************************************
  151.      *
  152.      *   bi_full_warp_loop(..
  153.      *
  154.      *   This routine sets up the coefficients
  155.      *   and loops through an entire
  156.      *   ROWSxCOLS image that is being warped.
  157.      *
  158.      *   This version of the routine uses bilinear
  159.      *   interpolation to find the gray shades.
  160.      *   It is more accurate than warp_loop,
  161.      *   but takes longer because of the floating
  162.      *   point calculations.
  163.      *
  164.      *******************************************/
  165.  
  166. bi_full_warp_loop(the_image, out_image,
  167.                   x1, x2, x3, x4,
  168.                   y1, y2, y3, y4,
  169.                   extra_x, extra_y)
  170.    int    extra_x, extra_y,
  171.           x1, x2, x3, x4,
  172.           y1, y2, y3, y4;
  173.    short  the_image[ROWS][COLS],
  174.           out_image[ROWS][COLS];
  175. {
  176.    double denom, di, dj,
  177.           xa, xb, xab, x_out, ya, yb, yab, y_out;
  178.    int    i, j;
  179.  
  180.    denom      = COLS * ROWS;
  181.  
  182.      /***********************************
  183.      *
  184.      *   Set up the terms for the
  185.      *   spatial transformation.
  186.      *
  187.      ***********************************/
  188.  
  189.    xa  = x2 - x1;
  190.    xb  = x4 - x1;
  191.    xab = x1 - x2 + x3 - x4;
  192.  
  193.    ya  = y2 - y1;
  194.    yb  = y4 - y1;
  195.    yab = y1 - y2 + y3 - y4;
  196.  
  197.      /***********************************
  198.      *
  199.      *   Loop through the image and
  200.      *   perform the spatial
  201.      *   transformation.
  202.      *
  203.      ***********************************/
  204.  
  205.       /* NOTE a=j b=i */
  206.  
  207.    printf("\n");
  208.    for(i=0; i<ROWS; i++){
  209.       if( (i%10) == 0) printf("%d ", i);
  210.       for(j=0; j<COLS; j++){
  211.  
  212.          di = (double)(i);
  213.          dj = (double)(j);
  214.  
  215.          x_out = x1 +
  216.                  (xa*dj)/COLS +
  217.                  (xb*di)/ROWS +
  218.                  (xab*di*dj)/(denom);
  219.          y_out = y1 +
  220.                  (ya*dj)/COLS +
  221.                  (yb*di)/ROWS +
  222.                  (yab*di*dj)/(denom);
  223.  
  224.          out_image[i+extra_y][j+extra_x] =
  225.           bilinear_interpolate(the_image, x_out, y_out);
  226.  
  227.       }  /* ends loop over j */
  228.    }  /* ends loop over i */
  229.  
  230. }   /* ends bi_full_warp_loop */
  231.